package logic

import (
	"context"
	"fmt"
	"github.com/jinzhu/copier"
	"xialou_platform/app/errorx"
	"xialou_platform/services/agent/model"

	"xialou_platform/services/system/rpc/internal/svc"
	"xialou_platform/services/system/rpc/systemClient"

	"github.com/zeromicro/go-zero/core/logx"
)

type SysConfigByKeyLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSysConfigByKeyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SysConfigByKeyLogic {
	return &SysConfigByKeyLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// SysConfigByKey 根据key查询系统配置
func (l *SysConfigByKeyLogic) SysConfigByKey(in *systemClient.KeyReq) (*systemClient.SysConfig, error) {
	// todo: add your logic here and delete this line
	sysConfig, err := l.svcCtx.SysConfigModel.FindOne(l.ctx, in.Key)
	if err != nil {
		if err == model.ErrNotFound {
			return nil, errorx.NewDefaultError(fmt.Sprint("找不到", in.Key))
		}
		return nil, err
	}
	res := &systemClient.SysConfig{}
	copier.Copy(res, sysConfig)
	res.CreateTime = sysConfig.CreateTime.Unix()
	res.UpdateTime = sysConfig.UpdateTime.Unix()
	return res, nil
}
